<template>
    <div >
        <div
            style="position: relative;  width: 100%; height: 70%; display: flex; justify-content: center;flex-wrap: wrap; overflow-x: hidden; align-items: center;">
            <div style="width: 100%; height:30rem; overflow: hidden;" class="imageContainer">
                <img src="@/assets/img/2.png" alt="" style="width: 100%; height: 100%; object-fit:cover;">
            </div>
            <div style="margin-top: 0; position: absolute; text-shadow: 2px 2px 10px #000; z-index: 1;">
                <h1
                    style="font-size: 2rem; color: #fff; font-weight: 500; padding: 0;border: 0;margin: 0;width: 100%; font-family:Ubuntu-Bt ;">
                    画廊</h1>
            </div>

        </div>
        <div class="photos-container" ref="scroll">
            <div class="masonry-container">
                <div class="masonry-tools">
                    <div class="photos-group">
                        <ul>
                            <li>
                                <span style="cursor: pointer;" class="active" @click="showAll">全部</span>
                            </li>
                            <li v-for="album in albums" :key="album.id">
                                <span style="cursor: pointer;" @click="showGroup(album.albumName)">{{ album.albumName }}</span>
                            </li>
                        </ul>
                    </div>
                    <div class="layout-buttons">
                        <div class="custom-button-group">
                            <button 
                                class="custom-button" 
                                :class="{ active: columns === 3 }"
                                @click="changeLayout(3)">
                                3列
                            </button>
                            <button 
                                class="custom-button" 
                                :class="{ active: columns === 4 }"
                                @click="changeLayout(4)">
                                4列
                            </button>
                        </div>
                    </div>
                </div>
                <div class="photos-content fancybox-content">
                    <div class="gallery" :style="galleryStyle">
                        <div class="gallery-item" :class="photo.albumName" v-for="photo in photos" :key="photo.id"
                            @mouseover="hoveredImageIndex=photo.id;" @mouseleave="hoveredImageIndex = null">
                            <el-image class="gallery-image" :src="photo.image" style="height: 100%; width: 100%;" 
                                :preview-src-list="photos.map(v => v.image)" fit="cover" lazy >
                                <div slot="placeholder" class="image-slot">
                                    <img src="https://blog.catouo.cn/assets/loading-BOl9TL6s.gif" alt="">
                                </div>
                            </el-image>
                            <div class="overlay" :class="{ 'show': hoveredImageIndex==photo.id }">
                                <div class="description">
                                    {{ photo.name }}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return{
            albums:[],
            photos:[],
            hoveredImageIndex: null,
            columns: 3, // 默认3列
        }
    },
    computed: {
        galleryStyle() {
            return {
                'grid-template-columns': `repeat(${this.columns}, minmax(0, 1fr))`,
                'grid-auto-rows': this.columns === 3 ? '400px' : '300px'
            }
        }
    },
    created(){
        this.load()
    },
    updated(){
        this.set()
    },
    mounted(){
       
    },
    methods:{
        load() {
            this.$request.get('/album').then(res => {
                this.albums = res.data;
            })
            this.$request.get('/photo').then(res => {
                this.photos = res.data;
            })
        },
        showAll() {
            this.$request.get('/photo').then(res => {
                this.photos = res.data;
            })
        },
        showGroup(albumName) {
            this.$request.get('/photo/list',{params: {
            albumName:albumName
          }}).then(res=>{
            this.photos=res.data
        })
        },

        set() {
            const groups=document.querySelectorAll('.photos-group ul li span')
            groups.forEach(group=>{
                group.addEventListener('click',()=>{
                    document.querySelectorAll('.photos-group ul li span')
                    .forEach(group => group.classList.remove('active'));
                    group.classList.add("active")
                })

            })
           
        },
        changeLayout(cols) {
            this.columns = cols;
        }
    }
}
</script>

<style scoped>
.gallery-item {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
    cursor: pointer;
}

.gallery-image {
    transition: transform 0.3s ease;
}

.gallery-item:hover .gallery-image {
    transform: scale(1.05);
}

.overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.6);
    display: flex;
    justify-content: center;
    align-items: center;
    opacity: 0;
    pointer-events: none;
    transition: all 0.3s ease;
    transform: translateY(10px);
}

.overlay.show {
    opacity: 1;
    transform: translateY(0);
}

.description {
    color: white;
    text-align: center;
    z-index: 1000;
    padding: 1rem;
    font-size: 1.1em;
    font-weight: 500;
    transform: translateY(20px);
    transition: transform 0.3s ease;
    text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.5);
}

.overlay.show .description {
    transform: translateY(0);
}

.photos-group ul li span:after {
    content: "";
    display: block;
    border-bottom: 2px solid orange;
    width: 0;
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    transform: scaleX(0);
    transform-origin: 50% 0;
    transition: transform .35s;
}

.photos-group ul li span:hover:after {
    width: 100%;
    transform: scale(1);
}

photos-group ul li span.active:after {
    width: 100%;
    transform: scale(1);
}

.entry-summary h3 {
    font-weight: 700;
    text-transform: uppercase;
    font-size: .9em;
    letter-spacing: .05em;
    transform: translate3d(0, -20px, 0);
    margin-bottom: .5em;
}

.gallery-caption {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    z-index: 10;
    pointer-events: none;
}

.entry-summary {
    display: inline-block;
    position: absolute;
    width: 100%;
    opacity: 0;
    margin: 0;
    transition: all .2s ease;
    text-align: center;
    bottom: 0;
    padding-bottom: 1em;
    font-size: .8em;
    color: #fff;
}

@media (max-width:768px) {
    .gallery {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    grid-auto-rows: 220px !important;
    height: min-content
}
    
}
.gallery {
    clear: both;
    max-width: 100%;
    display: grid;
    gap: 1rem !important;
    height: min-content;
}
.photos-container {
    margin: 50px 0;
    position: relative;
    z-index: 1;
}
.masonry-container {
    margin: auto 4vw;
}
.masonry-tools {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.layout-buttons {
    margin-left: auto;
}

.photos-group ul {
    list-style: none;
    padding: 0;
    margin: 0;
    font-size: 1em;
    letter-spacing: .2em;
}

.photos-group ul li {
    display: inline-block;
    vertical-align: bottom;
    margin: 0 2em 1.5em 0;
}

.photos-group ul li span {
    display: block;
    color: #666;
    position: relative;
}
.photos-group ul li span.active:after {
    width: 100%;
    transform: scale(1);
}
.photos-group ul li span:after {
    content: "";
    display: block;
    border-bottom: 2px solid orange;
    width: 0;
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    transform: scaleX(0);
    transform-origin: 50% 0;
    transition: transform .35s;
}

.photos-container .photos-inner .photos-content {
    width: 100%;
    justify-content: center;
}
.masonry-container .photos-content {
    margin: 1.5em auto;
}

.gallery {
    clear: both;
    max-width: 100%;
    margin: 0;
    opacity: 1;
    transform: translateZ(0);
    /* transition: transform, opacity .35s; */
}

figure {
    margin: 1em 40px;
}

.custom-button-group {
    display: flex;
    gap: 1px;
    background: #e0e0e0;
    padding: 1px;
    border-radius: 6px;
}

.custom-button {
    padding: 6px 16px;
    border: none;
    background: white;
    cursor: pointer;
    font-size: 14px;
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;
}

.custom-button:first-child {
    border-radius: 5px 0 0 5px;
}

.custom-button:last-child {
    border-radius: 0 5px 5px 0;
}

.custom-button.active {
    color: white;
    background: linear-gradient(45deg, #ff758c 0%, #6495ed 100%);
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.custom-button:not(.active):hover {
    background: linear-gradient(45deg, #ff8fa3 0%, #82a7f3 100%);
    color: white;
}

.custom-button::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(
        120deg,
        transparent,
        rgba(255,255,255,0.3),
        transparent
    );
    transition: 0.5s;
}

.custom-button:hover::before {
    left: 100%;
}
</style>
